package com.wxc.api.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxc.api.admin.aop.MyLog;
import com.wxc.api.admin.entity.SysUser;
import com.wxc.api.admin.mapper.SysUserMapper;
import com.wxc.api.admin.service.ISysUserService;
import com.wxc.common.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2022-04-14
 */

@RestController
@RequestMapping("/api.admin/sysUser")
@SaCheckRole("admin") // 角色认证：必须具有指定角色才能进入该方法
public class SysUserController {

    @Autowired
    private ISysUserService userService;

    @Resource
    private SysUserMapper userMapper;

    // 用户列表
    @MyLog(value = "获取用户列表")
    @RequestMapping("list")
    @SaCheckPermission(value = "sys:user:query", orRole = "admin") // 权限认证：必须具有指定权限才能进入该方法
    public Result list(
            @RequestParam(defaultValue = "1") Integer p,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(defaultValue = "") String keywords
    ) {

        Page<SysUser> page = new Page<>(p, size);
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        if (!"".equals(keywords)) {
            queryWrapper.like("user_name", keywords);
        }
        IPage<SysUser> iPage = userMapper.selectPage(page, queryWrapper);
        List<SysUser> records = page.getRecords();

        return Result.page(records,iPage);
    }

    // 用户信息
    @MyLog(value = "获取用户信息")
    @RequestMapping("info")
    @SaCheckPermission(value = "sys:user:query", orRole = "admin")
    public Result info() {
        SysUser userInfo = userService.info();
        return Result.success(userInfo);
    }

    // 用户添加
    @MyLog(value = "添加用户")
    @RequestMapping("add")
    @SaCheckPermission(value = "sys:user:add", orRole = "admin")
    public Result add(
            String account, String userName, String password,
            @Validated({SysUser.AddGroup.class}) SysUser sysUser
    ) {
        Integer id = userService.add(account, userName, password);
        if (id > 0) {
            return Result.success(id);
        }
        if (id == -1) {
            return Result.fail("用户已存在");
        }
        return Result.fail();
    }

    // 设置密码
    @MyLog(value = "设置密码")
    @RequestMapping("setPassword")
    @SaCheckPermission(value = "sys:user:edit", orRole = "admin")
    public Result edit(
            String password,
            @Validated() SysUser sysUser
    ) {
        if (userService.setPassword(password) == 1) {
            return Result.success();
        }
        return Result.fail();
    }

    // 启用禁用
    @MyLog(value = "启用禁用用户")
    @RequestMapping("enabled")
    @SaCheckPermission(value = "sys:user:edit", orRole = "admin")
    public Result enabled(
            Integer id, boolean enabled
    ) {
        if (id == 1) {
            return Result.fail("超级管理员不可以修改");
        }

        if (userService.enabled(id, enabled) == 1) {
            return Result.success();
        }
        return Result.fail();
    }

    // 用户删除
    @MyLog(value = "删除用户")
    @RequestMapping("del")
    @SaCheckPermission(value = "sys:user:deL", orRole = "admin")
    public Result del(
            Integer id,
            @Validated({SysUser.DelGroup.class}) SysUser sysUser
    ) {
        if (StpUtil.getLoginIdAsInt() == id) {
            return Result.fail("不可以删除自己");
        }

        if (id == 1) {
            return Result.fail("超级管理员不可以删除");
        }

        if (userService.del(id) == 1) {
            return Result.success();
        }
        return Result.fail();
    }

}
